package com.example.project.activity.mingxi;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.project.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class ItemShareActivity extends AppCompatActivity implements View.OnClickListener {
    String type,money,time,name;
    private TextView tvtime,tvtype,tvname,tvmoney,tvfx;
    private ImageView ivImg,ivqx;
    private View fxtp; //线性布局的图片
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_share);
        verifyStoragePermissions(ItemShareActivity.this);
        init();
    }

    //动态获取内存存储权限
    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }


    private void init() {
        ivqx=findViewById(R.id.iv_qx);
        ivqx.setOnClickListener(this);
        tvtime=findViewById(R.id.tv_time);
        tvtype=findViewById(R.id.tv_type);
        tvname=findViewById(R.id.tv_name);
        tvmoney=findViewById(R.id.tv_money);
        ivImg=findViewById(R.id.iv_img);
        ivImg.setOnClickListener(this);
        fxtp=findViewById(R.id.fx_tp);
        tvfx=findViewById(R.id.tv_fx);
        tvfx.setOnClickListener(this);

        Intent intent=getIntent();
        time=intent.getStringExtra( "time" );
        type=intent.getStringExtra( "type" );
        name=intent.getStringExtra( "name" );
        money=intent.getStringExtra( "money" );
        tvtime.setText(time);
        tvtype.setText(type);
        tvname.setText(name);
        tvmoney.setText(money);

        if(name.equals("支出")){
            ivImg.setImageResource(R.drawable.outone);
        }else{
            ivImg.setImageResource(R.drawable.inone);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_fx:
                try {
                    shareCard(fxtp );
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                break;
            case R.id.iv_qx:
                finish();
                break;
        }
    }



    /**
     * 截取指定View为图片
     *
     * @param view
     * @return
     * @throws Throwable
     */
    public static Bitmap captureView(View view) throws Throwable {
        Bitmap bm = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(bm));
        return bm;
    }
    //把bitmap转化为file
    public File bitMap2File(Bitmap bitmap) {
        String path = "";
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            path = Environment.getExternalStorageDirectory() + File.separator;//保存到sd根目录下
        }

        // File f = new File(path, System.currentTimeMillis() + ".jpg");
        File f = new File(path, Integer.valueOf(String.valueOf((new Date()).getTime() / 1000)) + ".jpg");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            bitmap.recycle();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return f;
        }
    }

    public void shareCard(View view) throws Throwable {
        File file = bitMap2File(captureView(findViewById(R.id.fx_tp)));
        if (file != null && file.exists() && file.isFile()) {
            //由文件得到uri
            Uri imageUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.project.fileprovider", file);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            shareIntent.setType("image/*");
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(shareIntent, "分享图片"));
        }
    }

}